package com.KKB.web;

import com.KKB.bean.Role;
import com.KKB.bean.User;
import com.KKB.service.RoleService;
import com.KKB.service.UserService;
import com.KKB.service.impl.RoleServiceImpl;
import com.KKB.service.impl.UserServiceImpl;
import com.KKB.util.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(urlPatterns = "/power/user/users")
public class UserServlet extends HttpServlet {
    private UserService service=new UserServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String method = req.getParameter("method");
        if ("select".equals(method)){
            select(req,resp);
        }else if ("insert".equals(method)){
            insert(req,resp);
        }else if ("getRoleList".equals(method)){
            getRoleList(req,resp);
        }else if ("findById".equals(method)){
            findById(req,resp);
        }else if ("delete".equals(method)){
            delete(req,resp);
        }else if ("update".equals(method)){
            update(req,resp);
        }
    }

    protected void select(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String index = req.getParameter("index");
        int pageIndex=(index==null||index.length()==0)?1:Integer.parseInt(index);

        PageUtil pageUtil = new PageUtil();
        List<User> userList = service.getUserList(pageIndex, pageUtil.getPageSize());
        int total = service.total();
        pageUtil.setTotal(total);
        pageUtil.setPageIndex(pageIndex);
        pageUtil.setDataList(userList);

        req.setAttribute("pi",pageUtil);
        req.getRequestDispatcher("list.jsp").forward(req,resp);

    }
    protected void insert(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String loginName = req.getParameter("loginName");
        String password = req.getParameter("password");
        String realName = req.getParameter("realName");
        String sex = req.getParameter("sex");
        String roleId = req.getParameter("roleId");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        String cardId = req.getParameter("cardId");
        String desc = req.getParameter("desc");

        User user = new User();
        user.setLoginName(loginName);
        user.setPassword(password);
        user.setRealName(realName);
        user.setSex(Integer.parseInt(sex));
        user.setRoleId(Integer.parseInt(roleId));
        user.setEmail(email);
        user.setPhone(phone);
        user.setAddress(address);
        user.setCardId(cardId);
        user.setDesc(desc);

        UserService usersService = new UserServiceImpl();
        int i = usersService.addUsers(user);

        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();

        if (i > 0) {
            writer.println("<script>alert('新建成功');location.href='/power/user/users?method=select';</script>");
        } else {
            writer.println("<script>alert('新建失败');location.href='/power/user/users?method=getRoleList';</script>");
        }
    }
    protected void getRoleList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        RoleService roleService = new RoleServiceImpl();
        List<Role> roleList = roleService.getList();
        req.setAttribute("rList", roleList);
        req.getRequestDispatcher("add.jsp").forward(req, resp);
    }
    protected void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uid = req.getParameter("uid");
        RoleService roleService = new RoleServiceImpl();
        List<Role> roleList = roleService.getList();

        UserService usersService = new UserServiceImpl();
        User user = usersService.findById(Integer.parseInt(uid));

        req.setAttribute("rList", roleList);
        req.setAttribute("u1", user);
        req.getRequestDispatcher("edit.jsp").forward(req, resp);
    }
    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String userId = req.getParameter("userId");
        String loginName = req.getParameter("loginName");
        String password = req.getParameter("password");
        String realName = req.getParameter("realName");
        String sex = req.getParameter("sex");
        String roleId = req.getParameter("roleId");
        String email = req.getParameter("email");
        String phone = req.getParameter("phone");
        String address = req.getParameter("address");
        String cardId = req.getParameter("cardId");
        String desc = req.getParameter("desc");

        User user = new User();
        user.setUserId(Integer.parseInt(userId));
        user.setLoginName(loginName);
        user.setPassword(password);
        user.setRealName(realName);
        user.setSex(Integer.parseInt(sex));
        user.setRoleId(Integer.parseInt(roleId));
        user.setEmail(email);
        user.setPhone(phone);
        user.setAddress(address);
        user.setCardId(cardId);
        user.setDesc(desc);

        UserService usersService = new UserServiceImpl();
        int i = usersService.update(user);
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i > 0) {
            writer.println("<script>alert('更新成功');location.href='/power/user/users?method=select';</script>");
        } else {
            writer.println("<script>alert('更新失败');location.href='/power/user/users?method=findById&uid="+userId+"';</script>");
        }
    }
    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uid = req.getParameter("uid");
        UserService usersService = new UserServiceImpl();
        int i = usersService.deleteById(Integer.parseInt(uid));
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter writer = resp.getWriter();
        if (i > 0) {
            writer.println("删除成功");
        } else {
            writer.println("删除失败");
        }
    }
}
